<?php
/**
 * Created by PhpStorm.
 * Admininfo: Administrator
 * Date: 2017/12/11
 * Time: 14:19
 */

namespace app\common\controller;
use app\admin\model\Message;
use think\captcha\Captcha;
use think\Config;
use think\Controller;
use think\Db;
use think\Hook;
use think\Request;
use think\Session;

class Baseadmin extends Controller
{
	function __construct (Request $request = null)
	{
        parent::__construct($request);
        Hook::listen('CheckAuth',$params);
	}
	/**
	 * @return bool
	 * 当前是否登录
	 */
    public function isLogin()
	{
		return  Session::get('userinfo.id')? true : false;
	}
	/**
	 * 获取验证码
	 */
	public  function 	captcha(){
	  $captcha=Config::get('captcha');
	  $Captchas=new  Captcha($captcha);
		return $Captchas->entry();
	}
	public 	function check_verify($code){
		$captcha = new Captcha();
		return $captcha->check($code);
	}
    /***
     *添加日志
     */
    protected  function adduserlog(){
        $data['messages']='管理员【'.Session::get('userinfo.name').'】'.date('Y-m-d H:i:s',time()).'登录后台系统！';
        $data['userid']=Session::get('userinfo.id');
        $data['addtime']=time();
        $data['status']=1;
        Db::name('adminlog')->insert($data,true);
    }

    /**
     * @param $type
     * @param $id
     * @param $title
     * @param $message
     */
    public function addmsg($type,$id,$title,$message){
        //类型  1 用户  2 商户
        if($type==1){
            $data['type']=1;
            $data['user_id']=$id;
        }elseif ($type==2){
            $data['type']=2;
            $data['shop_id']=$id;
        }
        $data['title']=$title;
        $data['message']=$message;
        $message=new Message();
        $message->save($data);
    }
    /**
     * @return mixed
     * 提出时间格式化的方法,以便别的方法调用
     */
    protected function settimes(){
        $begin=strtotime(input('begin'));
        $end=strtotime(input('end'));
        if($begin && $end) {
            if($begin>$end){
                $begin=$end;
                $end=strtotime(input('begin'));
                $where['create_time'] =['between',[$begin,$end]];
            }else{
                $where['create_time'] =['between',[$begin,$end]];
            }
        }else if (!$begin && $end){
            $begin=$end;
            $end=time();
            $where['create_time'] =['between',[$begin,$end]];
        }else if(input('begin') && !input('end')){
            $end=time();
            $where['create_time'] =['between',[$begin,$end]];
        }else{
            $begin= strtotime('-7 days');
            $end = time();
            $where['create_time'] =['between',[$begin,$end]];
        }
        $data['begin']=$begin;
        $data['end']=$end;
        return $data;
    }

    protected function settimess($begin,$end){
        if($begin && $end) {
            if($begin>$end){
                $begin=$end;
                $end=$begin;
                $where['create_time'] =['between',[$begin,$end]];
            }else{
                $where['create_time'] =['between',[$begin,$end]];
            }
        }else if (!$begin && $end){
            $begin=$end;
            $end=time();
            $where['create_time'] =['between',[$begin,$end]];
        }else if($begin && !$end){
            $end=time();
            $where['create_time'] =['between',[$begin,$end]];
        }else{
            $begin= strtotime('-7 days');
            $end = time();
            $where['create_time'] =['between',[$begin,$end]];
        }
        $data['begin']=$begin;
        $data['end']=$end;
        return $data;
    }
}